<template>
	<view class="address-form" :data-theme='theme()' :class="theme() || ''">
		<form @submit="formSubmit" @reset="formReset">
			<view style="margin: 20rpx;" class="bg-white p-0-30 f30">
				<view class="d-s-c border-b">
					<text class="key-name">收货人</text>
					<input class="ml20 flex-1 f32 p-30-0" name="name" type="text" placeholder-class="grary9"
						v-model="address.name" placeholder="请输入收货人姓名" />
				</view>
				<view class="d-s-c border-b">
					<text class="key-name">联系方式</text>
					<input class="ml20 flex-1 f32 p-30-0" name="phone" type="text" placeholder-class="grary9"
						v-model="address.phone" placeholder="请输入收货人手机号" />
				</view>
				<!-- 				<view class="d-s-c border-b">
					<text class="key-name">所在地区</text>
					<view class="input-box flex-1" @click="showMulLinkageThreePicker">
						<input class="ml20 f32 flex-1 p-30-0" type="text" placeholder-class="grary9" placeholder="" v-model="selectCity"
						 disabled="true"  />
					</view>
				</view> -->
				<view class="d-s-c border-b">
					<text class="key-name">详细地址</text>
					<view v-if="agreen == true" style="height: 80rpx;line-height: 80rpx;color:#ccc"
						@click="getMyLocation()" class="ml20 flex-1 p-30-0 lh150 f32">
						{{addressTips}}
					</view>
					<view  v-else @click="getMyLocation()">
						<textarea style="width: 400rpx;" class="ml20 flex-1 p-30-0 lh150 f32"  disabled="true" name="detail" :auto-height="true"
							v-model="address.detail" placeholder-class="grary9" placeholder="请选择街道小区楼牌号等"></textarea>
					</view>

				</view>
				<view class="d-s-c border-b">
					<text class="key-name">门牌号</text>
					<input class="ml20 flex-1 f32 p-30-0" name="key_num" type="text" placeholder-class="grary9"
						v-model="key_num" placeholder="请输入门牌号" />
				</view>
				<view class="d-b-c">
					<text class="key-name f32">设为默认地址</text>
					<view class="radio d-s-c p-30-0">
						<radio style="transform:scale(0.6)" color="#2568BC" :checked="is_default"
							@click="is_default = !is_default" />
					</view>
				</view>
				<view class="tips">
					将该地址作为每次下单时默认选择的地址
				</view>
			</view>
			<view class="p30"><button form-type="submit" class="theme-btn f32 mt60 addBtn">保存</button></view>
		</form>
		<mpvue-city-picker v-if="is_load" ref="mpvueCityPicker" :province="province" :city="city" :area="area"
			:pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				cityPickerValueDefault: [0, 0, 0],
				selectCity: '选择省,市,区',
				province_id: 0,
				city_id: 0,
				region_id: 0,
				address: {
					detail: "",
					phone: "",
					name: ""
				},
				delta: 1,
				province: [],
				city: [],
				area: [],
				is_load: false,
				is_default: false,
				latitude: '',
				longitude: '',
				key_num: '',
				agreen: false,
				addressTips: "请输入街道小区楼牌号等"
			};
		},
		onLoad: function(options) {
			this.delta = options.delta;
			this.getData();
			this.address.phone = uni.getStorageSync('mobile');
		},
		onShow() {
			console.log('666');
			var that = this
			// that.address.detail = '请输入街道小区楼牌号等'
			// that.getTabBarLinks();
			tt.getLocation({
				type: 'gcj02',
				success(res) {
					that.agreen = false
					console.log(`经度${res.longitude}纬度${res.latitude}`);
					that.city_name = uni.getStorageSync('city_name');
					if (that.city_name == '') {
						that.city_name = res.city
					} else {
						that.city_name = uni.getStorageSync('city_name');
					}
				},
				fail(res) {
					console.log("getLocation调用失败", res);
					that.agreen = true
					that.addressTips = '请输入街道小区楼牌号等'
				},
			});
		},
		methods: {
			getMyLocation() {
				let that = this
				console.log('6789');
				if (that.agreen == false) {
					uni.chooseLocation({
						success: (res) => {
							console.log("chooseLocation success: ", res);
							// this.message = res
							that.address.detail = res.address
							that.latitude = res.latitude
							that.longitude = res.longitude
							console.log(that.address.detail);
						},
						fail: (err) => {
							console.log("chooseLocation fail: " + err.errMsg);
						},
					});
				} else {
					console.log('678910');
					tt.showModal({
						title: "温馨提示",
						content: "获取定位失败，为了更好的为您服务，请授权定位权限",
						confirmColor: "#2263B3",
						confirmText: "去授权",
						success(res) {
							console.log('showModal success', res);
							if (res.confirm) {
								tt.openSetting({
									success: res => {
										console.log('打开设置页面成功: ', res.errMsg);
									},
									fail: res => {
										console.log('打开设置页面失败: ', res.errMsg);
									},
									complete: res => {
										console.log('接口已调用: ', res.errMsg);
									}
								});
							}
						},
						fail(err) {
							console.log('showModal fail', err);
						},
						complete(res) {
							console.log('showModal complete', res);
						}
					})

				}

			},

			// 获取省市区
			getData() {
				let self = this;
				self._post('settings/getRegion', {}, function(res) {
					self.province = res.data.regionData[0];
					self.city = res.data.regionData[1];
					self.area = res.data.regionData[2];
					self.is_load = true;
				});
			},
			/*提交*/
			formSubmit: function(e) {
				let self = this;
				let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
				var formdata = e.detail.value;
				console.log(formdata.detail, '888');
				formdata.province_id = self.province_id;
				formdata.latitude = self.latitude;
				formdata.longitude = self.longitude;
				formdata.city_id = self.city_id;
				formdata.region_id = self.region_id;
				formdata.is_default = self.is_default ? 1 : 0;
				formdata.detail = self.address.detail
				if (formdata.name == '') {
					uni.showToast({
						title: '请输入收货人姓名',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}

				if (formdata.phone == '') {
					uni.showToast({
						title: '请输入手机号码',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (!reg.test(formdata.phone)) {
					uni.showToast({
						title: '请输入正确的手机号码',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}


				if (formdata.detail == '') {
					uni.showToast({
						title: '请选择详细地址',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.key_num == '') {
					uni.showToast({
						title: '请输入门牌号',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				formdata.detail = formdata.detail + self.key_num;
				self._post('user.address/add', formdata, function(res) {
					setTimeout(() => {
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						});
					}, 0);
					setTimeout(() => {
						uni.navigateBack()
					}, 2000);

					// self.showSuccess(res.msg, function() {
					// 	// #ifndef H5
					// 	uni.navigateBack({
					// 		delta: parseInt(self.delta)
					// 	});
					// 	// #endif
					// 	// #ifdef H5
					// 	history.go(-self.delta);
					// 	// #endif
					// });
				});
			},

			formReset: function(e) {
				console.log('清空数据');
			},

			/*三级联动选择*/
			showMulLinkageThreePicker() {
				this.$refs.mpvueCityPicker.show();
				console.log('???');
			},

			/*确定选择的省市区*/
			onConfirm(e) {
				this.selectCity = e.label;
				this.province_id = e.cityCode[0];
				this.city_id = e.cityCode[1];
				this.region_id = e.cityCode[2];
			}
		}
	};
</script>

<style>
	page {
		/* background-color: #FFFFFF; */
	}

	.address-form {
		/* border-top: 16rpx solid #f2f2f2; */
	}

	.f32 {
		font-size: 28rpx !important;
	}

	.address-form .key-name {
		width: 220rpx;
		font-size: 28rpx
	}

	.address-form .btn-red {
		height: 88rpx;
		line-height: 88rpx;
		border-radius: 44rpx;
		box-shadow: 0 8rpx 16rpx 0 rgba(226, 35, 26, .6);
	}

	.addBtn {
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 40rpx;
		background-color: #2568BC;
		border: 1px solid #2568BC;
		color: #fff;
	}

	.tips {
		font-size: 24rpx;
		color: #ccc;
		margin-top: -20rpx;
		width: 100%;
		height: 60rpx;
	}
</style>